<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<head>
    <title>字典管理</title>
    <link rel="stylesheet" href="${ctx}/assets/admin/LingUI/css/ui.jqgrid.css"/>
</head>

<div class="row">
    <input type="text" id="selectItemId" style="display:none"/>
    <div class="col-sm-6">
        <div class="widget-box widget-color-blue2" style="height:600px;min-height:420px; height:auto!important">
            <div class="widget-header">
                <h4 class="widget-title lighter smaller">字典列表</h4>
            </div>
            <div id="search" style="padding: 8px"></div>
            <%--查询条件更多 --%>
            <div class="b_grid_line  col-lg-12"></div>                        <!-- 分隔线 -->
            <div class="widget-body">
                <div class="widget-main padding-8 admin-body">
                    <table id="item_list_table"></table>
                    <%--jqgrid数据表格 --%>
                    <div id="item_list_pager"></div>
                    <%--jqgrid表格 页脚 --%>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-6">
        <div class="widget-box widget-color-blue2" style="height:600px;min-height:420px; height:auto!important">
            <div class="widget-header">
                <h4 class="widget-title lighter smaller">字典子项列表</h4>
            </div>
            <div id="subItemsearch" style="padding: 8px"></div>
            <div class="b_grid_line  col-lg-12"></div>
            <div class="widget-body">
                <div class="widget-main padding-8 admin-body">
                    <table id="subitem_list_table"></table>
                    <%--jqgrid数据表格 --%>
                    <div id="subitem_list_pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var scripts = [null,
        "${ctx}/assets/admin/LingUI/js/jqGrid/jquery.jqGrid.js",
        "${ctx}/assets/admin/LingUI/js/jqGrid/i18n/grid.locale-cn.js",
        "${ctx}/assets/admin/LingUI/js/control.js",
        "${ctx}/assets/admin/LingUI/js/jsutil/js/jquery.filterSearch.js",   //筛选搜索
        null];
    var _grid_id = "#item_list_table";	//表格 选择器
    var _pager_id = "#item_list_pager";	//表格页脚 选择器
    ace.load_ajax_scripts(scripts, function () {
        $("#search").strongFilterSearch({
            inputFiled: "ITEM_NAME",				//搜索字段，不填默认为searchValue
            inputPlaceholder: "请输入字典名称等关键字",		//占位符
            searchCallback: callback//,				//搜索按钮点击后回调
        });

        $("#subItemsearch").strongFilterSearch({
            inputFiled: "ITEM_NAME",				//搜索字段，不填默认为searchValue
            inputPlaceholder: "请输入字典名称等关键字",		//占位符
            searchCallback: callback//,				//搜索按钮点击后回调
        });

        //调整以适应页面大小  其中 $(".page-content") 为jqgrid的父级容器
        $(window).off('resize.jqGrid').on('resize.jqGrid', function () {
            setTimeout(function () {
                $(_grid_id).jqGrid("setGridWidth", $(".admin-body").width());
                $("#subitem_list_table").jqGrid("setGridWidth", $(".admin-body").width());
                $(".ui-jqgrid-bdiv").width($(".admin-body").width() + 1);
            }, 50);
        });

        //搜索回调函数
        function callback(data) {
            jQuery("#item_list_table").jqGrid('setGridParam', {
                url: "${ctx}/${frameworkPath}/item/list?sort=ITEM_ORDER&&dir=asc",
                mtype: "POST",
                postData: data //传递的数据
            }).trigger("reloadGrid");
        }

        function callback_subItem(data) {
            jQuery("#subitem_list_table").jqGrid('setGridParam', {
                url: "${ctx}/${frameworkPath}/subitem/list?sort=SUB_ITEM_ORDER&&dir=asc",
                mtype: "POST",
                postData: data //传递的数据
            }).trigger("reloadGrid");
        }

        jQuery(_grid_id).jqGrid({
            mtype: "POST",
            url: "${ctx}/${frameworkPath}/item/list?sort=ITEM_ORDER&&dir=asc",
            colNames: ['编号', '字典名称', '操作'],
            colModel: [
                {name: 'id', index: 'id', width: 80, editable: true, hidden: false},
                {name: 'name', index: 'name', width: 60, editable: true},
                {
                    name: 'action',
                    index: '',
                    width: 30,
                    fixed: false,
                    sortable: false,
                    align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var s = '<div class=" action-buttons">'
                        s = s + '<a class="blue my-tooltip-link tooltip-primary" title="编辑" style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 " onclick="editItem(\'' + rowObject.id + '\')"></i></a>';
                        s = s + '<a class="red my-tooltip-link tooltip-primary" title="删除" style="cursor:pointer"><i class="ace-icon fa fa-trash-o bigger-130"  onclick="deleteItem(\'' + rowObject.id + '\')"></i></a>';
                        return s + '</div>';
                    }
                }
            ],
            pager: _pager_id, //分页信息 放置的位置
            page: lui.pageNum(_grid_id),
            onSelectRow: function (id) {
                $("#selectItemId").val(id);
                callback_subItem({parent: id});
            }
        });
        //自定义工具栏 =============BEGIN======================//
        jQuery(_grid_id)
                .jqGrid('navGrid', _pager_id)
                .navButtonAdd(_pager_id, {
                    title: "新增",//按钮悬浮提示
                    //iconTitle : "新增",//图标下面显示（不填的话默认为title里面的值）
                    buttonicon: "ace-icon fa fa-plus-circle purple",
                    onClickButton: function () {
                        lui.open({
                            title: '新增字典',
                            content: "${ctx}/${frameworkPath}/item/create",
                            area: ['50%', '400px'],
                            yes: function (index, layero) {
                                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                                var retObj = iframeWin.formValid();
                                if (retObj.text) {
                                    lui.info(retObj.text);
                                    if (retObj.state) {
                                        layer.close(index);
                                        jQuery(_grid_id).trigger("reloadGrid");
                                    }
                                }
                            }
                        });
                    }
                })
                .navButtonAdd(_pager_id, {
                    title: "编辑",
                    buttonicon: "ace-icon fa fa-pencil blue",
                    onClickButton: editItems
                })
                .navButtonAdd(_pager_id, {
                    title: "删除",
                    buttonicon: "ace-icon fa fa-trash-o red",
                    onClickButton: deleteItems
                })
                .navButtonAdd(_pager_id, {
                    title: "刷新",
                    buttonicon: "ace-icon fa fa-refresh orange",
                    onClickButton: function () {
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                });

        jQuery('#subitem_list_table').jqGrid({
            mtype: "POST",
            colNames: ['编号', '子项名称', '子项值', '排序字段', '操作'],
            colModel: [
                {name: 'id', index: 'id', width: 20, editable: true, hidden: false},
                {name: 'name', index: 'name', width: 60, editable: true},
                {name: 'value', index: 'value', width: 60, editable: true},
                {name: 'order', index: 'order', width: 20, editable: true, align: 'center'},
                {
                    name: 'action',
                    index: '',
                    width: 30,
                    fixed: false,
                    sortable: false,
                    align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var s = '<div class=" action-buttons">'
                        s = s + '<a class="blue my-tooltip-link tooltip-primary" title="编辑" style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 " onclick="editSubItem(\'' + rowObject.id + '\')"></i></a>';
                        s = s + '<a class="red my-tooltip-link tooltip-primary" title="删除" style="cursor:pointer"><i class="ace-icon fa fa-trash-o bigger-130"  onclick="deleteSubItem(\'' + rowObject.id + '\')"></i></a>';
                        return s + '</div>';
                    }
                }
            ],
            pager: '#subitem_list_pager', //分页信息 放置的位置
            page: lui.pageNum('#subitem_list_table')
        });

        //自定义工具栏 =============BEGIN======================//
        jQuery("#subitem_list_table")
                .jqGrid('navGrid', "#subitem_list_pager")
                .navButtonAdd("#subitem_list_pager", {
                    title: "新增",//按钮悬浮提示
                    //iconTitle : "新增",//图标下面显示（不填的话默认为title里面的值）
                    buttonicon: "ace-icon fa fa-plus-circle purple",
                    onClickButton: function () {
                        lui.open({
                            title: '新增子项',
                            content: "${ctx}/${frameworkPath}/subitem/create?parent=" + $("#selectItemId").val(),
                            area: ['50%', '400px'],
                            yes: function (index, layero) {
                                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                                var retObj = iframeWin.formValid();
                                if (retObj.text) {
                                    lui.info(retObj.text);
                                    if (retObj.state) {
                                        layer.close(index);
                                        jQuery("#subitem_list_table").trigger("reloadGrid");
                                    }
                                }
                            }
                        });
                    }
                })
                .navButtonAdd("#subitem_list_pager", {
                    title: "编辑",
                    buttonicon: "ace-icon fa fa-pencil blue",
                    onClickButton: editSubItems
                })
                .navButtonAdd("#subitem_list_pager", {
                    title: "删除",
                    buttonicon: "ace-icon fa fa-trash-o red",
                    onClickButton: deleteSubItems
                })
                .navButtonAdd("#subitem_list_pager", {
                    title: "刷新",
                    buttonicon: "ace-icon fa fa-refresh orange",
                    onClickButton: function () {
                        jQuery("#subitem_list_table").trigger("reloadGrid");
                    }
                });

    });

    //编辑数据
    function editItem(id) {
        lui.open({
            title: '编辑字典',
            content: "${ctx}/${frameworkPath}/item/edit?id=" + id,
            area: ['50%', '400px'],
            yes: function (index, layero) {
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                var retObj = iframeWin.formValid();
                if (retObj.text) {
                    lui.info(retObj.text);
                    if (retObj.state) {
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
            }
        });
    }

    //编辑字典
    function editItems() {
        var ids = $("#item_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.warn('至少选择一行数据');
            return;
        }
        else if (ids.length > 1) {
            lui.warn("最多选择一行数据");
            return;
        }
        editrow(ids[0]);
    }

    //删除
    function deleteItem(id) {
        lui.confirm("确定要删除吗？",
                function () {
                    var idArray = new Array();
                    idArray.push(id);
                    $.ajax({
                        type: 'POST',
                        url: '${ctx}/${frameworkPath}/item/delete',
                        traditional: true,
                        dataType: 'json',
                        data: {'ids': idArray, _method: 'DELETE'},
                        success: function (data) {
                            if (data.success) {
                                lui.success('删除成功');
                                jQuery(_grid_id).trigger("reloadGrid");
                            }
                            else {
                                lui.fail('删除失败');
                            }
                        },
                        error: function (XmlHttpRequest, textStatus, errorThrown) {
                            lui.fail('删除失败');
                        }
                    });
                },
                function () {
                }
        )
    }

    function deleteItems() {
        var ids = $("#item_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.warn('至少选择一行数据');
            return;
        }
        else if (ids.length > 1) {
            lui.warn("最多选择一行数据");
            return;
        }
        deleteItem(ids[0]);
    }

    function editSubItem(id) {
        lui.open({
            title: '编辑字典子项',
            content: "${ctx}/${frameworkPath}/subitem/edit?id=" + id + "&parent=" + $("#selectItemId").val(),
            area: ['50%', '400px'],
            yes: function (index, layero) {
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                var retObj = iframeWin.formValid();
                if (retObj.text) {
                    lui.info(retObj.text);
                    if (retObj.state) {
                        layer.close(index);
                        jQuery("#subitem_list_table").trigger("reloadGrid");
                    }
                }
            }
        });
    }

    function editSubItems() {
        var ids = $("#subitem_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.warn('至少选择一行数据');
            return;
        }
        else if (ids.length > 1) {
            lui.warn("最多选择一行数据");
            return;
        }
        editSubItem(ids[0]);
    }

    //删除
    function deleteSubItem(id) {
        lui.confirm("确定要删除吗？",
                function () {
                    var idArray = new Array();
                    idArray.push(id);
                    $.ajax({
                        type: 'POST',
                        url: '${ctx}/${frameworkPath}/subitem/delete',
                        traditional: true,
                        dataType: 'json',
                        data: {'ids': idArray},
                        success: function (data) {
                            if (data.success) {
                                lui.success('删除成功');
                                jQuery("#subitem_list_table").trigger("reloadGrid");
                            }
                            else {
                                lui.fail('删除失败');
                            }
                        },
                        error: function (XmlHttpRequest, textStatus, errorThrown) {
                            lui.fail('删除失败');
                        }
                    });
                },
                function () {
                }
        )
    }

    function deleteSubItems() {
        var ids = $("#subitem_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.warn('至少选择一行数据');
            return;
        }
        else if (ids.length > 1) {
            lui.warn("最多选择一行数据");
            return;
        }
        deleteSubItem(ids[0]);
    }

</script>